package com.example.s05_shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.s05_shop.pojo.Category;
import com.example.s05_shop.pojo.Product;
import com.example.s05_shop.service.ICategoryService;
import com.example.s05_shop.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
@RequestMapping("/admin/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ICategoryService categoryService;


//    @RequestMapping("/list")
//    public String list(Model m)
//    {
//        // 查询商品列表
//        // 放入model
//        return "admin/product/list";
//    }


    @RequestMapping("/list")
    public String list(@RequestParam(value = "page",required = true,defaultValue = "1") int page,
                       @RequestParam(value = "size",required = true,defaultValue = "10") int size,
                       Model m)
    {
        System.out.println("catgory list()");
        // 查询商品分类
//        List<Category> list = categoryService.list();
        // 页面信息
        IPage<Product> pageBean = new Page<>();
        pageBean.setCurrent(page);// 第几页
        pageBean.setSize(size);// 每页数量

        // 查询
        IPage<Product> pageInfo = productService.page(pageBean);

        // 放入model
//        m.addAttribute("list",list);
        m.addAttribute("pageBean",pageBean);

        // 查询商品分类信息
        List<Category> categories = categoryService.list();
        m.addAttribute("categories",categories);


        // 返回视图
        return "admin/product/list";
    }


    public static final String UPLOADED_FOLDER = "./upload/";

    @RequestMapping("/add")
    public String add(Product c,@RequestParam("file") MultipartFile file)
    {
        byte[] bytes = new byte[0];

        if(file!=null&& !file.isEmpty()) {
            try {
                bytes = file.getBytes();
                // UPLOADED_FOLDER 文件本地存储地址
                System.out.println(file.getOriginalFilename());

                String path1 = file.getOriginalFilename();
                path1 = path1.substring(path1.lastIndexOf("\\") + 1);
                String fileSavePath = UPLOADED_FOLDER + path1;

                Path path = Paths.get(fileSavePath);
                Files.write(path, bytes);

                System.out.println("文件名:"+file.getOriginalFilename());
                c.setPic("/images/" + file.getOriginalFilename());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


        // 调用service保存商品分类
        boolean result = productService.save(c);

        // 跳转商品列表页
        return "redirect:/admin/product/list";
    }
}